package mylogin

import (
	"fmt"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

type User struct {
	Username string `form:"username"`
	Password string `form:"password"`
	Hobby []string `form:"hobby"`
	Gender string `form:"gender"`
	City string `form:"city"`
}
type TestGetBindUser struct {
	Username string `form:"username"`
	Password string `form:"password"`
}
type TestGetBindUserLujing struct {
	Username string `uri:"username"`
	Password string `uri:"password"`
}

func TestGetBind(c *gin.Context) {
	var user TestGetBindUser
	err := c.ShouldBind(&user)
	if err != nil {
		log.Fatal(err)
	}
	c.String(200, "User: %s", user)
}

func TestGetBindlujing(c *gin.Context) {
	var user TestGetBindUserLujing
	err := c.ShouldBindUri(&user)
	if err != nil {
		log.Fatal(err)
	}
	c.String(200, "User: %s", user)
}



func MyHandler(c *gin.Context) {
	c.JSON(200, gin.H{
		"hello": "世界，你好。",
	})
}


func Login(c *gin.Context) {
	c.HTML(200, "login_bs.html", nil)

}

func Dologin(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	fmt.Println(username, password)
	c.HTML(200, "index.html", gin.H{
		"username": username,
		"password": password,
	})
}
func Register(c *gin.Context) {
	c.HTML(200, "register.html", nil)
}

func DoRegister(c *gin.Context) {

	//username := c.PostForm("username")
	//password := c.PostForm("password")
	//city := c.PostForm("city")
	//gender := c.PostForm("gender")
	//hobby := c.PostFormArray("hobby")
	//c.String(200, "Username: %s, Password: %s, Gender: %s, city: %s, hobby: %s", username, password, gender, city, hobby)
	// 绑定 Form表单
	var user User
	c.ShouldBind(&user)
	c.String(200, "User: %s", user)
}

func MyMiddleware1(c *gin.Context) {
	fmt.Println("我的第一个中间件")
}
func MyMiddleware2(c *gin.Context) {
	fmt.Println("我的第二个中间件")
}

// 私人数据
var secrets = gin.H{
	"foo": gin.H{"email": "111.com", "phone": "12345"},
	"foo2": gin.H{"email": "1222221.com", "phone": "122222345"},
	"foo3": gin.H{"email": "999911.com", "phone": "12676666345"},
}

// cookie
func HandlerCookie(c *gin.Context) {
	s, err := c.Cookie("username")
	if err != nil {
		fmt.Println("不存在cookie")
		s = "张大力"
		c.SetCookie("username", s, 60*60, "/", "localhost", false, true)
	}
	fmt.Println(s)
	c.String(200, "测试cookie")
}

func WithLogin() {
	e := gin.Default()
	// session
	store := cookie.NewStore([]byte("secret"))
	e.Use(sessions.Sessions("mysession", store))
	e.GET("/hello", func(c *gin.Context) {
		session := sessions.Default(c)
		if session.Get("hello") != "world" {
			session.Set("hello", "world")
			session.Save()
		}
		c.JSON(200, gin.H{"hello": session.Get("hello")})
	})

	// 中间件
	e.Use(MyMiddleware1, MyMiddleware2)

	e.LoadHTMLGlob("mylogin/*")
	e.Static("/assets", "./assets")
	e.StaticFS("/croot", http.Dir("c:/"))
	e.StaticFile("/favicon.ico", "./assets/favicon.ico")
	e.GET("/login", Login)
	e.POST("/login", Dologin)
	e.GET("/register", Register)
	e.POST("/register", DoRegister)
	// 绑定查询参数
	e.GET("/testGetBind", TestGetBind)
	// 绑定路径参数
	// http://localhost:9999/testGetBindlujing/ghz/123
	e.GET("/testGetBindlujing/:username/:password", TestGetBindlujing)

	// BasicAuth 中间件，用户只能访问自己的资源
	authorized := e.Group("/admin", gin.BasicAuth(gin.Accounts{
		"foo": "bar",
		"foo1": "1234",
		"manu": "34444",
	}))
	// http://localhost:9999/admin/secrets

	authorized.GET("/secrets", func(c *gin.Context) {
		user := c.MustGet(gin.AuthUserKey).(string)
		fmt.Println(user)
		if secret, ok := secrets[user]; ok {
			c.JSON(200, gin.H{"user": user, "secret": secret})
		} else {
			c.JSON(200, gin.H{"user": user, "secret": "NO SECRET :("})
		}
	})
	// cookie测试 http://localhost:9999/test
	e.GET("/test", HandlerCookie)

	e.Run(":9999")
}
